package d0330;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.util.stream.Collector;

public class Test {
    //初始化文件内容
    public static void initFile(String filePath, List<String> userLists) {

        File file = new File(filePath);
        if (userLists.size() < 1) {
            System.out.println("用户列表不能为空");
            return;
        }
        try {
            FileWriter writer = new FileWriter(file);
            int size = userLists.size();
            Random random = new Random();
            for (int i = 0; i < 100; i++) {
                StringBuffer buffer = new StringBuffer();
                //往空字符串中追加人名，通过list集合的get（index）返回list集合的元素
                buffer.append(userLists.get(random.nextInt(size)));
                buffer.append(",");
                //往同一个字符串中追加消费金额
                buffer.append(random.nextInt(1000));
                buffer.append("\r\n");
                writer.write(buffer.toString());
            }
            writer.write("null");
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addName(String filePath) {
        ArrayList <String> userlist = new ArrayList <String>();
        userlist.add("张三");
        userlist.add("李四");
        userlist.add("张三");
        initFile(filePath, userlist);
    }

    public static void searchUser(String filePath) {
        try {
            FileReader reader = new FileReader(filePath);
            BufferedReader br = new BufferedReader(reader);
            String maxName = null;
            String str = null;
            //  key是名字 list里面放该用户每次消费的金额  list.size该用户出现的次数
            HashMap<String, List<Integer>> map = new HashMap<>();
            while(!(str = br.readLine()).equals("null")) {
                String[] split = str.split(",");
                String name = split[0];
                Integer money = Integer.valueOf(split[1]);
                if(map.containsKey(name)) {
                    //如果map容器内出现过某个用户的处理流程
                    List<Integer> list = map.get(name);
                    list.add(money);
                    map.put(name, list);
                }else{
                    //如果map容器内出现过某个用户的处理流程
                    ArrayList <Integer> integers = new ArrayList <Integer>();
                    integers.add(money);
                    map.put(name,integers );
                }
            }
            /* list.size表示该用户消费的次数
             */
            Iterator<Map.Entry<String,List<Integer>>> iterator = map.entrySet().iterator();
            Integer num = 0;
            while(iterator.hasNext()) {

                Map.Entry<String,List<Integer>> next = iterator.next();
                String name1 = next.getKey();
                List<Integer> moneyNum = next.getValue();
                if(moneyNum.size()>num) {

                    num = moneyNum.size();
                    maxName = name1;

                }
            }
            System.out.println("消费次数最多的用户是："+maxName);
            /*
             * 通过遍历消费次数最多的的那个用户的消费记录，
             * 寻找出消费次数最多的的那个用户的消费金额最大的那次
             */
            List<Integer> list2 = map.get(maxName);
            Iterator<Integer> iterator1 = list2.iterator();
            Integer maxMoney = 0;

            while(iterator1.hasNext()) {

                Integer next = iterator1.next();
                if(next  > maxMoney  ) {
                    maxMoney = next;
                }
            }
            System.out.println("消费次数最多的用户消费金额最多一次是："+maxMoney);
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String filePath = "D:/initfile.txt";
        addName( filePath);
        searchUser(filePath);
    }
}